package com.lizemin.beanfactorypostprocessor.processors;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

/**
 * @author lzm
 * @date 2025/9/27 11:02
 * @description
 */
@Slf4j
@Component
public class BeanConfigPostProcessor implements BeanFactoryPostProcessor {

    /**
     * 修改bean的属性值
     */
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        BeanDefinition beanDefinition = beanFactory.getBeanDefinition("dog");
        MutablePropertyValues mpv = beanDefinition.getPropertyValues();
        PropertyValue[] propertyValues = mpv.getPropertyValues();
        log.info("下面开始打印dog这个bean的所有属性值");
        // 因为Dog实例是通过@bean工厂方法创建的，这种方式创建的实例的属性值不会放到PropertyValues中，所以这里打印的属性值是空的
        for (PropertyValue propertyValue : propertyValues) {
            log.info("{} => {}", propertyValue.getName(), propertyValue.getValue());
        }
        log.info("开始修改dog的属性值");
        mpv.addPropertyValue("name", "小黑");
    }

}
